﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DataAccess;

namespace Delivery
{
    public partial class frmThamSoKetNoi : DevExpress.XtraEditors.XtraForm
    {
        MyRegistry _myRegistry = new MyRegistry();
        public frmThamSoKetNoi()
        {
            InitializeComponent();            
        }
         

        private void btnThoat_Click(object sender, EventArgs e)
        {
                Application.Exit();
        }            
       
        private void txtPassWord_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
               btnKiemTra_Click(sender,e);
            }
        }

        private void btnKiemTra_Click(object sender, EventArgs e)
        {
            if (txtDBName.Text.Trim().Length == 0 || txtDBName.Text.Trim().Length == 0)
                XtraMessageBox.Show("Bạn nhập thiếu thông tin!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                bool _isConnected = false;
                System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
                conn.ConnectionString = "Data Source=" + txtServerName.Text.Trim() + ";Initial Catalog=" + txtDBName.Text.Trim() + ";Persist Security Info=True;User ID=" + txtUserName.Text.Trim() + ";Password=" + txtPassWord.Text.Trim();
                try
                {
                    conn.Open();
                    _isConnected = true;
                }
                catch (Exception)
                {
                    XtraMessageBox.Show("Không kết nối được Database!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    conn.Close();
                }
                if (_isConnected)
                {
                    this.Close();
                    _myRegistry.RegSetValue("ServerName", txtServerName.Text.Trim());
                    _myRegistry.RegSetValue("DatabaseName", txtDBName.Text.Trim());
                    _myRegistry.RegSetValue("UserName", txtUserName.Text.Trim());
                    _myRegistry.RegSetValue("Password", txtPassWord.Text.Trim());

                    Program.ServerName = txtServerName.Text.Trim();
                    Program.DatabaseName = txtDBName.Text.Trim();
                    Program.UserName = txtUserName.Text.Trim();
                    Program.Password = txtPassWord.Text.Trim();

                    this.Hide();

                    frmDangNhap dn = new frmDangNhap();
                    dn.ShowDialog();
                }
            }
        }

        private void frmThamSoKetNoi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                btnThoat_Click(sender, e);
            if (e.KeyCode == Keys.Enter)
                btnKiemTra_Click(sender, e);
        }
     
    }
}